package com.zzyl.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.properties.WechatConfigProperties;
import com.zzyl.service.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class WechatServiceImpl implements WechatService {

    @Autowired
    private WechatConfigProperties wechatConfigProp;

//    获取openid的路径 GET https://api.weixin.qq.com/sns/jscode2session
    private final String GET_OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session";
//    获取accessToken的路径 GET https://api.weixin.qq.com/cgi-bin/token
    private final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
//    获取手机号的路径 POST https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN
    private final String GET_POST_PHONE_NUM_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    @Override
    public String getOpenId(String code) {
//        获取微信的配置
        Map<String,Object> paramMap = getWechatParamMap();
        paramMap.put("js_code",code);
        paramMap.put("grant_type","authorization_code");
//        调用微信接口，获取openId
        String jsonStr = HttpUtil.get(GET_OPEN_ID_URL, paramMap);
        log.info("获取openId的jsonStr：{}",jsonStr);
//        把json字符串转对象
        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        if (jsonObject==null){
            log.error("获取openid失败");
            throw new BaseException(BasicEnum.GET_OPENID_ERROR);
        }
        String openid = jsonObject.getStr("openid");
        log.info("openid={}",openid);
        return openid;
    }

    @Override
    public String getPhoneNum(String phoneCode) {
//        获取access_token
        String accessToken = getAccessToken();
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("code",phoneCode);
//        获取手机号
        String jsonStr = HttpUtil.post(GET_POST_PHONE_NUM_URL+accessToken,JSONUtil.toJsonStr(paramMap));
        log.info("获取手机号的jsonStr：{}",jsonStr);
//        把json字符串转对象
        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        if (jsonObject==null){
            log.error("获取手机号失败");
            throw new BaseException(BasicEnum.GET_PHONE_ERROR);
        }
        JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
        if (phoneInfo==null){
            log.error("获取手机号失败");
            throw new BaseException(BasicEnum.GET_PHONE_ERROR);
        }
        String phoneNumber = phoneInfo.getStr("phoneNumber");
        log.info("phoneNumber={}",phoneNumber);
        return phoneNumber;
    }

    /**
     * 获取微信的访问token
     * @return
     */
    private String getAccessToken() {
        Map<String,Object> paramMap = getWechatParamMap();
        paramMap.put("grant_type","client_credential");
//        调用微信接口，获取token
        String jsonStr = HttpUtil.get(GET_ACCESS_TOKEN_URL, paramMap);
        log.info("获取accessToken的jsonStr：{}",jsonStr);
        if (StringUtils.isBlank(jsonStr)){
            log.error("获取accessToken失败");
            throw new BaseException(BasicEnum.GET_WECHAT_ACCESS_TOKEN_ERROR);
        }
//        json转对象
        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        if (jsonObject==null){
            log.error("获取accessToken失败");
            throw new BaseException(BasicEnum.GET_WECHAT_ACCESS_TOKEN_ERROR);
        }
        String accessToken = jsonObject.getStr("access_token");
        log.info("accessToken={}",accessToken);
        return accessToken;
    }

    private Map<String,Object> getWechatParamMap(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("appid",wechatConfigProp.getAppid());
        map.put("secret",wechatConfigProp.getSecret());
        return map;
    }
}
